package ltd.followkba.page;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.core.content.FileProvider;

import com.kcbsdk.ARoomType;
import com.kcbsdk.EventHandler;
import com.kcbsdk.MyManage.AUserFileType;

import java.io.File;
import java.nio.ByteBuffer;
import java.util.Timer;
import java.util.TimerTask;

import ltd.advskin.C;
import ltd.advskin.MRAM;
import ltd.advskin.MSKIN;
import ltd.advskin.U;
import ltd.advskin.base.BaseList;

import ltd.advskin.base.KcListHolder;
import ltd.advskin.base.KcListListener;
import ltd.advskin.base.KcTypeListener;
import ltd.advskin.base.MPAGE;
import ltd.advskin.media.AdvMedia;
import ltd.advskin.media.advm;
import ltd.advskin.task.TaskFree;
import ltd.advskin.task.TaskSysUpdate;
import ltd.advskin.utils.SubUtils;
import ltd.advskin.utils.SysTools;
import ltd.advskin.view.KcHeaderView;
import ltd.advskin.view.KcListView;
import ltd.followkba.BuildConfig;
import ltd.followkba.MAPP;
import ltd.followkba.MDBS;
import ltd.followkba.R;
import ltd.followkba.W;
import main.CApiUtils;
import main.MAPI;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
import static ltd.advskin.MSKIN.MLOGE;
import static ltd.advskin.MSKIN.MSTRING;
import static ltd.advskin.MSKIN.MTOAST;
import static main.MAPI.mMyManage;
import static main.MAPI.mMyTools;
import static main.MAPI.mMyUtils;
import static main.MAPI.mMyWorks;

public class SettingPage extends BaseList {
    private RelativeLayout rlClearCacheSetting;
    private RelativeLayout mFeedBackLayout;
    private RelativeLayout mNightLayout;
    private TextView tvAppVersion;
    private Timer mTimer;
    private ByteBuffer mRunState;
    private float gPercent;

    private String[] gsSettingText = MSKIN.getStringArray(R.array.setting_text);

    public void MLOG(String text) {
        MSTRING(MAPI.GET_LOG_NAME(this) + text);
    }

    @Override
    public void onInitView() {
        setLayout(R.layout.normal_list_activity);
        mKcHeaderView = (KcHeaderView) findViewById(R.id.khvNormalList);
        mKcHeaderView.setTitle(getString(R.string.setting), new KcTypeListener() {
            @Override
            public void onMessage(int type) {
                switch (type) {
                    case KcHeaderView.TYPE_CLICK_LEFT:
                        setNext(null);
                        break;

                }
            }
        });
        initListView(findViewById(R.id.klvNormalList));
    }

    @Override
    public void onInitData(){                                                                       // onInitView结束，开始加载数据
            MAPP.setTaskPause(true);

        setSize(gTabSelect,gsSettingText.length + 1 + 1);
        // 退出登录
        mMyManage.setUiEventCallBack(new EventHandler() {
            @Override
            public void onComplete(int methodType, boolean isFail, String message) {
                if (methodType > 0) {
                    if (isFail) {
                        MSKIN.showMethodError(message, methodType);
                    } else {
                        if (mMyManage.isSuccessLogoutSvr(methodType)) {  // 成功退出登录,从服务器返回的结果
                            MTOAST(getString(R.string.success_logout), 2000);
//                            MSKIN.setMemoryBoolean(W.LoginActivity_Force, false);    // 为了强制退出的使用
                            MRAM.gMsgNewestQty = 0;
                            setNext(null);
                        }
                    }
                }
            }
        });
        mMyWorks.setUiEventCallBack(new EventHandler() {
            @Override
            public void onComplete(int methodType, boolean isFail, String message) {
                if (methodType > 0) {
                }else {
                    if (mMyWorks.isEventDataRefresh()){
                        MRAM.gServerNumber = mMyUtils.getServerNumber();
//                        MLOG("SettingPageA isEventDataRefresh  "+MRAM.gServerNumber);
                        setListView(gTabSelect);
                    }
                }
            }
        });
        MRAM.mTaskSysUpdate = new TaskSysUpdate("FollowKBa");
        String[] tabFile = {"arealist", "xxx_m00", "xxx_f00"};         // .md5 及.zip
        String cpuType = MRAM.mTaskSysUpdate.getCpuType();           // 先搞清楚自己CPU是什么类型
        tabFile[tabFile.length-2] = String.format("%s_m00", cpuType);              // 根据CPU的类型自动找对应的文件
        tabFile[tabFile.length-1] = String.format("%s_f00", cpuType);              // 根据CPU的类型自动找对应的文件
        MRAM.mTaskSysUpdate.fileFromServer(tabFile, false);
    }
    @Override
    public void onFinish() {       // 使用者选择性继承，用户按了后退键，不继承就直接关闭了
        setNext(null);
    }
    private void setNext(Object object){
        setPageResult(object);
        MAPP.setTaskPause(false);
        setClose();
    }


    private String[] getRemoteVersion() {
        String localFile = String.format("%ssystem.ini", MRAM.gsCachePath);
        if (localFile != null) {
            CApiUtils cApiUtils = new CApiUtils();
            String[] strings = new String[2];
            strings[0] = cApiUtils.readPrivateString("1_v", localFile);
            if (strings[0] != null) {
                SubUtils subUtils = new SubUtils();
                StringBuffer stringBuffer = new StringBuffer();
                for (int index = 0; index < 99; index++) {
                    String text = cApiUtils.readPrivateString(String.format("1_%s", index), localFile);
                    text = subUtils.getFileUtf8(text);
                    if (!MAPI.isEmpty(text)) {
                        stringBuffer.append(String.format("%s、%s\n", index, text));
                    }else {
                        break;
                    }
                }
                strings[1] = stringBuffer.toString();
                return strings;
            }
        }
        return null;
    }

    private String downloadFile() {
        String localFile = String.format("%ssystem.ini", MRAM.gsCachePath);
        CApiUtils cApiUtils = new CApiUtils();
        String remoteVer = cApiUtils.readPrivateString("1_v", localFile);
        String remoteMd5 = cApiUtils.readPrivateString("m0_0", localFile);
        String localVer = MSKIN.getMemoryString(W.apk_version, false);
        String localMd5 = MSKIN.getMemoryString(W.apk_file_md5, false);
        MLOG(String.format("SettingPageA DA %s(%s)%s(%s)", remoteVer, remoteMd5, localVer, localMd5));
        if (!MAPI.isEmpty(remoteVer) && !MAPI.isEmpty(remoteMd5)) {
            String appName = cApiUtils.readPrivateString("0_0", localFile);
            if (appName != null) {
                String unZipPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/";//String.format("%slib/", MRAM.gsRootPath);
                String apkFile = String.format("%s%s.apk", unZipPath, appName);
                String md5File = String.format("%s%s.md5", unZipPath, appName);
                boolean existFile = false;
                MLOG(String.format("SettingPageA DB %s(%s)", appName, unZipPath));
                if (!MAPI.isEmpty(appName)) {
                    existFile = MAPI.GET_FILE_LENGTH(apkFile) > 1;               // 解压后的文件存在?
                    if (existFile) {
                        existFile = MAPI.GET_FILE_LENGTH(md5File) > 1;               // 解压后的文件存在?
                    }
                }
                MLOG(String.format("SettingPageA DC %s(%s)%s(%s)%s", existFile, remoteVer, localVer, remoteMd5, localMd5));
//                MLOG(String.format("SettingPageA DC1 %s(%s)", remoteVer.equals(localVer), remoteMd5.equals(localMd5)));
                if (existFile && remoteVer.equals(localVer) && remoteMd5.equals(localMd5)) {
                    return apkFile;
                }
                String remoteFile = String.format("%s/%s.zip", appName, appName);
                String zipFile = String.format("%s%s.zip", MRAM.gsCachePath, remoteMd5);
                String result = "ok";
                MLOG(String.format("SettingPageA DD %s(%s)", remoteFile, zipFile));
                if (!MAPI.IS_FILE_DONE(zipFile)) {
                    int type = (ARoomType.ROOM_USER.getType() << 10);
                    result = mMyUtils.threadDownloadFiles(type, 0, AUserFileType.FILE_KCB_SETUP.getType(), 0, 0, remoteFile, zipFile, mRunState);
                    MLOG(String.format("SettingPageA DF %s(%s)%s", result, zipFile, remoteFile));
                }
                if ("ok".equals(result)) {
                    TaskSysUpdate task = new TaskSysUpdate();
                    if (task.upZipWithPath(zipFile, unZipPath)) {  // 解压zip到hsav根目录
                        MAPI.SET_FILE_DONE(zipFile);
                        MSKIN.setMemoryString(W.apk_version, remoteVer, false);
                        MSKIN.setMemoryString(W.apk_file_md5, remoteMd5, false);
                        MLOG(String.format("SettingPageA DG %s(%s)%s(%s)", MAPI.GET_FILE_LENGTH(apkFile), MAPI.GET_FILE_LENGTH(md5File), apkFile, md5File));
                        return apkFile;
                    }
                }
            }
        }
        return null;
    }

    private void getRemoteApkFile() {
        if (mTimer == null) {
            mRunState = ByteBuffer.allocateDirect(128);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final String fileName = downloadFile();
                    if (fileName != null) {
                        MRAM.mActivity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
//                                String fileName = String.format("%slib/followkba.apk", MRAM.gsRootPath);
                                installApkFile(MRAM.mContext, fileName);
                            }
                        });
                    }
                    if (mTimer != null) {
                        mTimer.cancel();
                        mTimer = null;
                        mRunState = null;
                    }

                }
            }).start();
            mTimer = new Timer();
            mTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    MRAM.mActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mRunState != null) {
                                byte[] data = mRunState.array();
                                int offset = MAPI.BUFF8_DWORD(MRAM.gByteAdj + 4, data);
                                int fileLeng = MAPI.BUFF8_DWORD(MRAM.gByteAdj + 12, data);
                                float percent = (int) (((float)offset / (float)fileLeng) * 1000.0f);
                                MLOG(String.format("SettingPageA A percent %s %s/%s", percent, offset, fileLeng));
                                if (gPercent != percent){
                                    gPercent = percent;
                                    setListView(gTabSelect);
                                }
                            }
                        }
                    });
                }
            }, 300, 300);
        }
    }
    private void installApkFile(Context context, String fileName) {
        File apkFile = new File(fileName);
        Intent intent = new Intent();
        try {
            // 判断是否是AndroidN以及更高的版本
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {           // 兼容7.0
                MLOG(String.format("SettingPageA KA %s(%s)%s", MAPI.GET_FILE_LENGTH(fileName), fileName, BuildConfig.APPLICATION_ID));
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                // 添加这一句表示对目标应用临时授权该Uri所代表的文件
                Uri apkUri = FileProvider.getUriForFile(context, context.getPackageName()+".provider", apkFile);
//                MLOG(String.format("SettingPageA KB %s(%s)", MAPI.GET_FILE_LENGTH(fileName), apkUri.toString()));
                intent.setDataAndType(apkUri , "application/vnd.android.package-archive");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            // 兼容8.0
                    boolean hasInstallPermission = context.getPackageManager().canRequestPackageInstalls();
                    if (!hasInstallPermission) {
                        startInstallPermissionSettingActivity();
                        return;
                    }
                }
            } else {
                MLOG(String.format("SettingPageA KB %s(%s)", MAPI.GET_FILE_LENGTH(fileName), fileName));
                MTOAST("准备升级文件B " + fileName, 2000);
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            MTOAST("BBBBB ", 2000);
            MLOG(String.format("SettingPageA KC %s(%s)", MAPI.GET_FILE_LENGTH(fileName), fileName));
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            MLOGE(String.format("installApkFile Error: %s", e.toString()));
        }
    }
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void startInstallPermissionSettingActivity() {
        // 注意这个是8.0新API 跳转到设置-允许安装未知来源-页面
        Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        MRAM.mContext.startActivity(intent);
    }


//    AndroidStudio：apk exposed beyond app through Intent.getData()_所以还是劝你学习的博客-CSDN博客
//    https://blog.csdn.net/weixin_42415553/article/details/96440013
    //    android升级安装包--包解析错误 - 简书
//    https://www.jianshu.com/p/04e14a1e957b
//    如何在Android7.0系统下通过Intent安装apk - 一座小楼的专栏 - CSDN博客
//    https://blog.csdn.net/czhpxl007/article/details/53781464
//下载安装APK(兼容Android7.0) - 于连林520wcf的专栏 - CSDN博客
//    https://blog.csdn.net/yulianlin/article/details/52775160

    private void installApkFile(String fileName) {

        Context context = MRAM.mContext;
        File apkFile = new File(fileName);
        Intent intent = new Intent();
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        intent.setAction(android.content.Intent.ACTION_VIEW);
//        intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
//        MRAM.mActivity.startActivity(intent);

        //判断是否是AndroidN以及更高的版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            MLOG(String.format("SettingPageA KA %s(%s)", MAPI.GET_FILE_LENGTH(fileName), fileName));
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            try {
                String ss = "com.example.administrator.devicemanagerapp" + ".fileprovider";
                Uri contentUri = FileProvider.getUriForFile(context.getApplicationContext(), ss, apkFile);
                intent.setAction(Intent.ACTION_VIEW);
                intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
                MTOAST("BBBBB ", 2000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            MLOG(String.format("SettingPageA KB %s(%s)", MAPI.GET_FILE_LENGTH(fileName), fileName));
            MTOAST("准备升级文件B " + fileName, 2000);
            intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        try {
//            MTOAST("准备升级文件C ");
            MLOG(String.format("SettingPageA KC %s(%s)", MAPI.GET_FILE_LENGTH(fileName), fileName));
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 通过隐式意图调用系统安装程序安装APK
     */
    public static void install(Context context) {
        File file = new File(
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                , "myApp.apk");
        Intent intent = new Intent(Intent.ACTION_VIEW);
        // 由于没有在Activity环境下启动Activity,设置下面的标签
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 24) { //判读版本是否在7.0以上
            //参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致   参数3  共享的文件
            Uri apkUri =
                    FileProvider.getUriForFile(context, "com.a520wcf.chapter11.fileprovider", file);
            //添加这一句表示对目标应用临时授权该Uri所代表的文件
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(file),
                    "application/vnd.android.package-archive");
        }
        context.startActivity(intent);
    }

    private int getVersionCode() {
        PackageManager packageManager = MRAM.mContext.getPackageManager();
        PackageInfo packageInfo;
        try {
            packageInfo = packageManager.getPackageInfo(MRAM.mContext.getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }

    private String dateTimeString(int dateTime) {
        int tmp1 = (dateTime >> 16) / (12 * 31);
        int tmp2 = (dateTime >> 16) % (12 * 31);
        int tmp3 = dateTime & 0xffff;
        int tmp4 = tmp3 % (60 * 30);
        return String.format("%02d/%02d/%02d %02d:%02d:%02d", (tmp1 + 2010) - 2000, (tmp2 / 31) + 1, (tmp2 % 31) + 1, tmp3 / (60 * 30), tmp4 / 30, tmp4 % 30);
    }

    private String versionString(int version) {
        if (version > 1000000) {
            return String.format("%d.%d.%d", version / 1000000, (version % 1000000) / 1000, (version % 1000000) % 1000);
        } else {
            return String.format("%d.%d", version / 1000, version % 1000);
        }
    }
    private String getVersionDateTime(int version, int dateTime) {
        return String.format("V%s build:%s", versionString(version), dateTimeString(dateTime));
    }
    private String getLibkcbsvrVersion() {
        String text = mMyTools.getLibkcbsvrVersion();
        if (text !=null){
            String[] split = text.split(",");
            if (split.length >= 2) {
                return getVersionDateTime(MAPI.STRING_DEC32(split[1]), MAPI.STRING_DEC32(split[0]));
            }
        }
        return null;
    }
    private String getMediaVersion(boolean video) {
        AdvMedia advMedia = new AdvMedia();
        String text = advMedia.getVersion(true);
        String error = "";
        if (text == null) {
            text = advm.versionString();
            error = "(E)";
        }
        if (text != null) {
            String[] split = text.split(",");
            if (split.length >= 4) {
                if (!video) {
                    return getVersionDateTime(MAPI.STRING_DEC32(split[1]), MAPI.STRING_DEC32(split[0])) + error;
                }
                return getVersionDateTime(MAPI.STRING_DEC32(split[3]), MAPI.STRING_DEC32(split[2])) + error;
            }
        }
        return null;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // KcListView的全部处理，放在页面的最后位置统一处理 ///////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    private final int[] item_setting_view = new int[]{
            R.layout.item_setting_view, 16,
//            <!--第一种类型：文字+小图标，共三个View-->
            R.id.llTypeSetting, R.id.tvTypeSetting, R.id.ivSetting,
//            <!--第二种类型：一个KcBtnProgress，共一个View-->
            R.id.kbpSignOutSetting,
//            <!--第三种类型：LOGO 版本号 公司名称-->
            R.id.llLogoSetting, R.id.ivLogoSetting,
            R.id.tvVersionNameSetting, R.id.tvVersionCodeSetting,
            R.id.tvKcbSdkSetting,
            R.id.tvAdvMediaSetting,
            R.id.tvKcCoreSetting,
            R.id.kbtSvrNumber,
            R.id.tvInfoSetting,
            R.id.kbtRemoteVersion,
            R.id.tvItemVersion,
            R.id.tvCopyrightSetting,
    };


    private void deleteFolder(File folder){
        File[] files = folder.listFiles();     // 文件夹下的所有文件及文件夹
        if (files != null){
            for (int index = 0; index < files.length; index++){
                if (files[index].isDirectory()) {
                    deleteFolder(files[index]);
                }else {
                    files[index].delete();
                }
            }
            folder.delete();
        }
    }

    private void initListView(final View view){
        mKcListView = new KcListView[1];
        mKcListView[0] = (KcListView)view;
        mKcListView[0].initView(null, item_setting_view, null, new KcListListener() {
            @Override
            public int onMessage(int type, int tab, Object object){
                return 0;
            }
            @Override
            public int onBindItemHolder(boolean skipImage, int tab, int position, KcListHolder holder) {
                String gsLocal_1;
                if (position < gsSettingText.length) {           // <!--第一种类型：文字+小图标，共三个View-->
                    holder.mView[0].setVisibility(View.VISIBLE);
                    holder.mView[3].setVisibility(View.GONE);
                    holder.mView[4].setVisibility(View.GONE);
                    gsLocal_1 = gsSettingText[(position) % gsSettingText.length];
                    MSKIN.setText(gsLocal_1, holder.mView[1]);
                    MSKIN.setBitmap(C.chose_arrow, holder.mView[2]); // 设置变色图片

                    holder.mView[0].setTag(position);
                    holder.mView[0].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            int position = (int) v.getTag();
                            switch (position){
                                case 0:                     // 皮肤设置
                                    ThemeStylePage themeStylePage = new ThemeStylePage();
                                    MPAGE.startBasePage(themeStylePage);
                                    break;
                                case 1:                     // 使用说明
                                    MAPP.startTipsPage();
                                    break;
                                case 2:                     // APP运行状态
                                    //                                    RunStatePage runStatePage = new RunStatePage();
//                                    MPAGE.startBasePage(runStatePage);
//                                    LogcatPage logcatPage = new LogcatPage();
//                                    logcatPage.mBaseInput = new BaseInput();
//                                    MPAGE.startBasePage(logcatPage);

                                    break;
                                case 3:                     // 清空所有缓存
                                    MTOAST("准备清空所有缓存，请稍候... ", 3000);
                                    new TaskFree(null, new TaskFree.Listener() {
                                        @Override
                                        public void onMessage(boolean isThread, Object object) {
//                                            MLOGE("清空所有缓存A");
                                            if (isThread){
                                                File folder = new File(MDBS.gsRootPath);
                                                deleteFolder(folder);
                                                return;
                                            }
                                            setNext(null);
                                            MTOAST("已经清空所有缓存 ", 3000);
                                        }
                                    });
                                    break;
                                case 4:                     // 删除所有账号
                                    new TaskFree(null, new TaskFree.Listener() {
                                        @Override
                                        public void onMessage(boolean isThread, Object object) {
                                            if (isThread){
                                                File folder = new File(MDBS.getAccountPath());
                                                deleteFolder(folder);
                                                return;
                                            }
                                            MTOAST("已经删除所有账号 "+MDBS.gsRootPath);
                                            setNext(this);
                                        }
                                    });

                                    break;
                                case 5:                     // 操作号登录
                                    MAPP.startLoginManagePage();
                                    break;
                            }
                        }
                    });
                }else if (position == gsSettingText.length) {               //   <!--第二种类型：一个KcBtnProgress，共一个View-->
                    holder.mView[0].setVisibility(View.GONE);
                    holder.mView[4].setVisibility(View.GONE);
                    holder.mView[3].setVisibility(View.VISIBLE);
                    holder.mView[3].setTag(position);
                    holder.mView[3].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            MSKIN.startAlertDialog(getString(R.string.sure_to_quit_account),getString(R.string.exit_logon),getString(R.string.cancel),getString(R.string.sure),new KcTypeListener() {
                                @Override
                                public void onMessage(int type) {
                                    if (type == 2){         // 确定按钮
                                        MSTRING(String.format("logoutSvr MSKIN.getCurrentUserID() %s", MSKIN.getCurrentUserID()));
                                        mMyManage.logoutSvr(MSKIN.getCurrentUserID());
                                    }else  {               // 取消按钮
                                    }
                                }
                            });
                        }
                    });
                } else {                                   // <!--第三种类型：LOGO 版本号 公司名称-->
                    holder.mView[0].setVisibility(View.GONE);
                    holder.mView[3].setVisibility(View.GONE);
                    holder.mView[4].setVisibility(View.VISIBLE);
                    SysTools sysTools = new SysTools();
                    gsLocal_1 = String.format("版本号：V%s", sysTools.getVersionString());
                    MSKIN.setText(gsLocal_1, holder.mView[6]);
                    int aa=     0x0fe643ad;
                    MLOG("aa "+aa);
                    gsLocal_1 = String.format("build： %s", dateTimeString(getVersionCode()));
                    MSKIN.setText(gsLocal_1, holder.mView[7]);


                    MSKIN.setText(String.format("KcbSdk: %s", getLibkcbsvrVersion()), holder.mView[8]);
                    MSKIN.setText(String.format("Media : %s", getMediaVersion(false)), holder.mView[9]);
                    MSKIN.setText(String.format("Video : %s", getMediaVersion(true)), holder.mView[10]);
                    TaskSysUpdate taskSystemUpdate = new TaskSysUpdate();
                    MSKIN.setText(String.format("服务器#%s", MRAM.gServerNumber), holder.mView[11]);
                    holder.mView[11].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            MTOAST("尝试更换服务器", 2000);
                            mMyTools.changeFileServer();
                        }
                    });
                    MSKIN.setText(String.format("%sx%s(%s) [%d]%s", MRAM.g4ScreenWidth, MRAM.g4ScreenHeight, MRAM.gDensity, MRAM.gByteBuffer, taskSystemUpdate.getCpuType()), holder.mView[12]);
//                    MSKIN.setText(MSKIN.getString(R.string.copyright_company_information), holder.mView[14]);

                    String[] version = getRemoteVersion();
                    if (version != null && version.length > 0) {
                        if (gPercent == 0) {
                            gsLocal_1 = String.format("更新版本 V%s", version[0]);
                        }else {
                            gsLocal_1 = String.format("V%s 下载：%s%%", version[0], gPercent/10);
                        }
                        if (!version[0].equals(sysTools.getVersionString())){        // 版本不相同
                            holder.mView[13].setVisibility(View.VISIBLE);
                            MSKIN.setText(gsLocal_1, holder.mView[13]);
                            if (version.length > 1) {
                                holder.mView[13].setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        MTOAST("准备下载更新版本", 2000);
                                        getRemoteApkFile();
                                    }
                                });
                                MSKIN.setText(version[1], holder.mView[14]);
                                holder.mView[14].setVisibility(View.VISIBLE);
                            }else {
                                holder.mView[14].setVisibility(View.GONE);
                            }
                        }else {
                            holder.mView[13].setVisibility(View.GONE);
                            MSKIN.setText(gsLocal_1, holder.mView[14]);
                        }
                    }
                }
                return 0;
            }
        });
    }

}
